﻿using Beauty.BLL;
using Beauty.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebUI.Models;

namespace WebUI.Controllers
{
    public class UserController : Controller
    {
        // GET: User
        public ActionResult Login()
        {
            UtilWebService util = new UtilWebService();
            //加载类别
            picclassBLL classes = new picclassBLL();
            List<picclass> list = util.GetAllAlbumClass();
            ViewBag.classes = list;
            return View();
        }
        [HttpPost]
        public ActionResult Login(string username, string password)
        {
            bool isAutoLogin = true;
            if (string.IsNullOrEmpty(Request["auto_login"]))
            {
                isAutoLogin = false;
            }
            else if (Request["auto_login"] == "false")
            {
                isAutoLogin = false;
            }
            user user;
            ResData res = new ResData();
            bllUser.Login(username, password, username, out user, res);
            if (res.resCode == 200)
            {
                Session[DataConfig.UserSessionName] = user;
                if (isAutoLogin)
                {
                    HttpCookie cookies = new HttpCookie(DataConfig.AutoLoginCookie);
                    cookies.Values.Add("name", username);
                    cookies.Values.Add("pass", password);
                    cookies.Expires = DateTime.Now.AddDays(7);
                    Response.AppendCookie(cookies);
                }
                return Json(new { erro_code = 0, msg = "success" });
            }
            else
            {
                return Json(new { erro_code = 1, msg = res.resMsg });
            }

        }

        userBLL bllUser = new userBLL();

        public ActionResult Register()
        {
            UtilWebService util = new UtilWebService();
            //加载类别
            picclassBLL classes = new picclassBLL();
            List<picclass> list = util.GetAllAlbumClass();
            ViewBag.classes = list;
            return View();

        }


        [HttpPost]
        public ActionResult Register(string uname, string pwd, string mobile, string code)
        {
            ResData res = new ResData();
            List<user> user = bllUser.GetModelList("UName='" + uname + "'");
            if (user.Count > 0)
            {
                res.resCode = 201;
                res.resMsg = "该用户名已被使用";
            }
            else
            {
                //if (Session[DataConfig.VerifyCode].ToString() == code)
                //{

                bool flag = bllUser.Add(new Beauty.Model.user()
                {
                    UName = uname,
                    UPass = pwd,
                    UMobile = mobile,
                    IconUrl = "/Pic/user_headimg/img_default_avatar.png"
                });
                if (flag)
                {
                    res.resCode = 200;
                    res.resMsg = "注册成功";
                    Session.Remove(DataConfig.VerifyCode);//移出保存在服务器上的验证码 避免浪费资源
                }
                else
                {
                    res.resCode = 203;
                    res.resMsg = "注册失败，请重试";
                }
                //}
                //else
                //{
                //    res.resCode = 202;
                //    res.resMsg = "验证码不正确";

                //}
            }
            return Json(res);
        }
        [HttpPost]
        public ActionResult GetCode(string phone)
        {
            ResData res = new ResData();
            List<user> user = bllUser.GetModelList("UMobile='" + phone + "'");
            if (user.Count > 0)
            {
                res.resCode = 201;
                res.resMsg = "该手机号已被占用";
            }
            else
            {
                Session[DataConfig.VerifyCode] = 666;//将来这里存放手机验证码和用户填写的对比
                res.resCode = 200;
                res.resMsg = "发送成功，请注意查看验证码------------测试请填666";
            }
            return Json(res);
        }
        public ActionResult Test()
        {
            return View();
        }
    }
}